package com.fyp.MediaPlayer;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

public class MediaPlayerLibrary {
	
	

	boolean mStartPlaying = true;
	boolean mStartRecording = true;
	private MediaPlayer   mPlayer = null;
	
	private MediaRecorder mRecorder = null;
		private String Directory_Name ;
		private String extention = ".3gp";
	public static boolean playing;
	public static boolean isPlaying() {
		return playing;
	}

	public static void setPlaying(boolean playing) {
		MediaPlayerLibrary.playing = playing;
	}

	public MediaPlayerLibrary()
	{
		this(Environment.getExternalStorageDirectory().getAbsolutePath(),".3gp");
	}
	
	public MediaPlayerLibrary(String directory_name)
	{
		this(directory_name,".3gp");
	}
	
	public MediaPlayerLibrary(String directory_name, String extention)
	{
		setDirectory_Name(directory_name);
		setExtention(extention);
		
	}
	
	@SuppressWarnings("finally")
	public MediaPlayer startPlaying(String fileName) 
	{
	    mPlayer = new MediaPlayer();
	    try {
	    	setMediaStartPlaying(true);
	        mPlayer.setDataSource(getDirectory_Name()+"/"+fileName+getExtention());
	        mPlayer.prepare(); 
	        mPlayer.start();
	      
	    } catch (IOException e) 
	    {
	        Log.e("Media Player", "prepare() failed");
	        setMediaStartPlaying(false);
	        return null;
	    }
	    finally
	    {
	    	
	    	return mPlayer;	
	    		
	    	
	    }
	}
	@SuppressWarnings("finally")
	public static MediaPlayer startPlayingFromRaw(Context context,int resid)  
	{
		final MediaPlayer getedPlayer = MediaPlayer.create(context, resid);
	    try {
	    	
	    	setPlaying(true);
	        
	    	
	    	getedPlayer.start();
	    	getedPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					getedPlayer.stop();
					getedPlayer.release();
					setPlaying(false);
					
				}
			});
	    }
	    catch(IllegalStateException e)
	    {
	    	e.printStackTrace();
	    	getedPlayer.stop();
	    	getedPlayer.release();
	    }
	    finally
	    {
	    	
	    	return getedPlayer;	
	    		
	    	
	    }
	}
	public void startRecording(String fileName) {
		setMediaStartRecording(true);
	    mRecorder = new MediaRecorder();
	    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
	    mRecorder.setOutputFile(getDirectory_Name()+"/"+fileName+getExtention());
	    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

	    try
	    {
	        mRecorder.prepare();
	    } 
	    catch (IOException e) 
	    {
	        Log.e("Media Recorder", "prepare() failed");
	        setMediaStartRecording(false);
	    }

	    mRecorder.start();
	}

	public void stopRecording(MediaRecorder mRec) throws IllegalStateException {
		 if (mRec != null) {
			 mRec.stop();
			 mRec.release();
			 mRec = null;
		    }
		 else
		 {
			 if (mRecorder != null) 
			 {
				 setMediaStartRecording(false);
				 mRecorder.stop();
				 mRecorder.release();
				 mRecorder = null;
			    }
		 }

	   
	}

	public void stopPlaying(MediaPlayer mPlay) throws NullPointerException{
		 if (mPlay != null) {
			 mPlay.stop();
			 mPlay.release();
			 mPlay = null;
		    }
		 else
		 {
			 if (mPlayer != null) 
			 {
				 setMediaStartPlaying(false);
				 mPlayer.stop();
				 mPlayer.release();
				 mPlayer = null;
			    }
		 }
	}
	
	//============================================

	public MediaPlayer getMediaPlayer() {
		return mPlayer;
	}

	public void setmPlayer(MediaPlayer mPlayer) {
		this.mPlayer = mPlayer;
	}

	public MediaRecorder getMediaRecorder() {
		return mRecorder;
	}

	public void setMediaRecorder(MediaRecorder mRecorder) {
		this.mRecorder = mRecorder;
	}

	/**
	 * @return the directory_Name
	 */
	public String getDirectory_Name() {
		return Directory_Name;
	}

	/**
	 * @param directory_Name the directory_Name to set
	 */
	public void setDirectory_Name(String directory_Name) {
		Directory_Name = directory_Name;
	}

	/**
	 * @return the extension of file name
	 */
	public String getExtention() {
		return extention;
	}

	/**
	 * @param extention the extension of <code>File</code> set
	 */
	public void setExtention(String extention) {
		this.extention = extention;
	}
	public boolean isMediaPlaying() {
		return mStartPlaying;
	}

	public void setMediaStartPlaying(boolean mStartPlaying) {
		this.mStartPlaying = mStartPlaying;
	}

	public boolean isMediaRecording() {
		return mStartRecording;
	}

	public void setMediaStartRecording(boolean mStartRecording) {
		this.mStartRecording = mStartRecording;
	}
}
